function multiSelectController($rootScope, $scope, staffService, $filter) {
  'ngInject';

  this.view = {
    keyword: '',
    // msModel: [],
    requesting: false,
    isNullResult: false,
  };

  this.resource = {
    searchResult: [],
  };

  this.add = (item) => {
    this.msModel.push(item);
  };

  this.remove = (item) => {
    const index = this.msModel.indexOf(item);
    this.msModel.splice(index, 1);
  };

  const onAjaxSearchKeywordChange = (keyword) => {
    this.view.isNullResult = false;
    if (keyword) {
      this.view.requesting = true;
      staffService
      .query({ mapParams: { fullName: keyword, orgId: $rootScope.orgId || '0ed5e18dafa24e3c84a81814745804fd' } })
      .then((res) => {
        this.resource.searchResult = res.rows;
        if (res.rows.length === 0) {
          this.view.isNullResult = true;
        } else {
          this.view.isNullResult = false;
        }
      })
      .finally(() => {
        this.view.requesting = false;
      });
    }
  };

  const onOptionsSearchKeywordChange = (keyword) => {
    // $scope.$apply(() => {
    //   const res = $filter('filter')($scope.$parent[$scope.matchResult.collection], keyword);
    //   // const res = $filter('filter')([1, 2, 3, 4, '333'], '3');
    //   this.searchResult = res;
    //   console.log($scope.$ctrl.searchResult);
    // });
  };

  this.$onInit = () => {
    $scope.$watch(() => this.view.keyword,
      this.msOptions ? onOptionsSearchKeywordChange : onAjaxSearchKeywordChange);
  };
}

export default multiSelectController;
